<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="策略名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <j-dict-select-tag v-model="form.title" placeholder="请输入策略名称" dictCode="app_state_condition"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="升级说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="form.remark" placeholder="请输入升级说明"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="版本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="version">
              <a-input v-model="form.version" placeholder="请输入版本号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="升级方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mode">
              <j-dict-select-tag v-model="form.mode" placeholder="请输入升级方式1:推荐升级  2:强制升级" style="width: 100%"
                                 dictCode="app_upgrade_type"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="指定源版本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="srcVersionId">
              <a-input-number v-model="form.srcVersionId" placeholder="请输入指定源版本" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="升级渠道" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channel">
              <j-dict-select-tag v-model="form.channel" placeholder="请输入升级渠道0:全部1:app store2:蒲公英3:fir"
                                 style="width: 100%" dictCode="app_upgrade_channel"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <a-input v-model="form.url" placeholder="请输入链接"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <j-dict-select-tag v-model="form.state" placeholder="请输入状态1:未启动2:已启动" style="width: 100%"
                                 dictCode="app_state"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否定时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isTiming">
              <j-dict-select-tag v-model="form.isTiming" placeholder="请输入是否定时" style="width: 100%" dictCode="yn"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="定时启动时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择定时启动时间" v-model="form.startTime" :trigger-change="true" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">-->
          <!--            <a-form-model-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">-->
          <!--              <j-date placeholder="请选择创建时间" v-model="form.createTime" :trigger-change="true" style="width: 100%"/>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <!--          <a-col :span="24">-->
          <!--            <a-form-model-item label="修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="updateTime">-->
          <!--              <j-date placeholder="请选择修改时间" v-model="form.updateTime" :trigger-change="true" style="width: 100%"/>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="策略启动条件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="condition">
              <j-dict-select-tag v-model="form.condition" placeholder="请输入策略启动条件：1：立即，2：手动，3：定时" style="width: 100%"
                                 dictCode="app_state_condition"/>
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, getAction} from '@/api/manage'

import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'

export default {
  name: 'CmsAppVersionForm',
  components: {
    JFormContainer,
    JDate,
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: {
        appId: '',
        type: 1,
        title: '',
        remark: '',
        version: '',
        mode: '',
        srcVersionId: '',
        channel: '',
        url: '',
        state: '',
        isTiming: '',
        startTime: '',
        createTime: '',
        updateTime: '',
        condition: '',
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {
        title: {
          rules: [
            {required: true, message: '请输入策略名称!'},
          ]
        },
        remark: {
          rules: [
            {required: true, message: '请输入升级说明!'},
          ]
        },
        version: {
          rules: [
            {required: true, message: '请输入版本号!'},
          ]
        },
        url: {
          rules: [
            {required: true, message: '请输入链接!'},
          ]
        },
      },
      url: {
        list: "/common/cmsAppVersion/list",
        add: "/common/cmsAppVersion/add",
        edit: "/common/cmsAppVersion/edit",
        queryById: "/common/cmsAppVersion/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    this.form.appId = this.$route.query.appId;
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
  },
  methods: {
    add() {
      this.edit({
        appId: "",
        type: '',
        title: '',
        remark: '',
        version: '',
        mode: '',
        srcVersionId: '',
        channel: '',
        url: '',
        state: '',
        isTiming: '',
        startTime: '',
        createTime: '',
        updateTime: '',
        condition: '',
      })
    },
    edit(record) {
      this.form = Object.assign({}, record)
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = {id: this.formData.dataId};
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.form.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }

          this.form.appId = this.$route.query.appId;
          let formData = Object.assign({}, this.form)
          console.log("表单提交数据111111", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {

              that.$message.success(res.message);
              that.$emit('ok');
              // that.queryByAppId();

            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    //根据AppId查询List
    queryByAppId() {
      this.form = {}
      this.form.appId = this.$route.query.appId;
      let formData = Object.assign({}, this.form)
      console.log("表单提交数据22222", formData)
      getAction(this.url.list, formData).then((res) => {

        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false;
      })
    },
    popupCallback(row) {
      this.form = row
    },
  }
}
</script>
